<?php
X::import('Application.Modles.CommandShowAsText');

class CShowShowASText extends CommandShowAsText{
	
	/**
	 * @var CShowResult
	 */
	public $result;
	/**
	 * @param CShowResult $result
	 */
	function __construct($result){
		$this->result = $result;
	}
	
	/**
	 * 
	 */
	public function show(){
		if ( $this->result->hasError() ){
			echo $this->result->error;
			return;
		}
		
		$showHandler = 'show'.$this->result->type;
		if ( method_exists($this, $showHandler) ){
			$this->$showHandler();
		}
		else {
			throw new Exception('');
		}
	}
	
	const PROJECT_ID_SHOW_LENGTH = 4;
	/**
	 * ProjectId 	ProjectName
	 * 1			project01
	 * 2			project02
	 * ....
	 */
	private function showProjectList(){
		foreach ( $this->result->result as $result ) {
			echo str_pad($result['id'], self::PROJECT_ID_SHOW_LENGTH).' '.$result['name']."\n";
		}
	}
	
	const TODO_ID_SHOW_LENGTH = 6;
	/**
	 * TodoId 	TodoName
	 * 1		todo1
	 * 2		todo2
	 * ....
	 */
	private function showTodoList(){
		foreach ( $this->result->result as $result ) {
			echo str_pad($result['id'], self::TODO_ID_SHOW_LENGTH).' '.$result['subject']."\n";
		}
	}
	
	/**
	 * ID 		: {$ID}
	 * Subject  : {$subject}
	 * Describe : 
	 * {$describe}
	 */
	private function showTodo(){
		$result = $this->result->result[0];
		echo 'ID       : '.$result['id']."\n";
		echo 'Subject  : '.$result['subject']."\n";
		echo 'Describe : '."\n";
		echo $result['describe']."\n";
	}
	
	/**
	 * ID 	: {$ID}
	 * Name : {$Name}
	 */
	private function showProject(){
		$result = $this->result->result[0];
		echo 'ID   : '.$result['id']."\n";
		echo 'Name : '.$result['name']."\n";
	}
	
	/**
	 * {$attr} : {$value}
	 */
	private function showTodoAttr(){
		$result = $this->result->result[0];
		foreach ( $result as $key => $value ){
			echo $key.' : '.$value."\n";
		}
	}
	
	/**
	 * {$attr} : {$value}
	 */
	private function showProjectAttr(){
		$result = $this->result->result[0];
		foreach ( $result as $key => $value ){
			echo $key.' : '.$value."\n";
		}
	}
}